﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using QBWeb.Models.CurriculumFolder;
using QBWeb.Models.QuestionFolder;

namespace QBWeb.Models
{
    public class DbInitializer:DropCreateDatabaseIfModelChanges<QBDbContext>
    {
         /// <summary>
        /// 创建初始管理员账号
        /// </summary>
        /// <param name = "db" ></param>
        // private void InitAdmin(QBDbContext db)
        //{
        //    const string adminName = "admin";
        //    const string adminPassword = "123456";
        //    const string adminRoleName = "admin";
        //    // 创建用户  
        //    var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));

        //    var user = new ApplicationUser { UserName = adminName };
        //    userManager.Create(user, adminPassword);
        //    //昵称
        //    var userNickName = new UserNameTb()
        //    {
        //        UserId = user.Id,
        //        NickName = adminName
        //    };
        //    db.UserNameTbs.Add(userNickName);
        //    db.SaveChanges();

        //    // 创建角色  
        //    var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));

        //    roleManager.Create(new IdentityRole("teachers"));
        //    roleManager.Create(new IdentityRole("students"));
        //    var adminRole = roleManager.Create(new IdentityRole(adminRoleName));
        //    // 给用户赋予角色  
        //    userManager.AddToRole(user.Id, adminRoleName);
        //    db.SaveChanges();
        //}
        protected override void Seed(QBDbContext context)
        {
            const string adminName = "admin";
            const string adminPassword = "123456";
            const string adminRoleName = "admin";
            // 创建用户  
            var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));

            var user = new ApplicationUser { UserName = adminName };
            userManager.Create(user, adminPassword);
            //昵称
            var userNickName = new UserNameTb()
            {
                UserId = user.Id,
                NickName = adminName,
            };
            context.UserNameTbs.Add(userNickName);
            context.SaveChanges();

            // 创建角色  
            var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));

            roleManager.Create(new IdentityRole("teachers"));
            roleManager.Create(new IdentityRole("students"));
            roleManager.Create(new IdentityRole(adminRoleName));
            // 给用户赋予角色  
            userManager.AddToRole(user.Id, adminRoleName);

            /****************************************************************************/
            context.QuestTypeTbs.AddRange(new List<QuestTypeTb>{
                new QuestTypeTb
                {
                    Id = 1,
                    TypeName ="选择题"
                },
                new QuestTypeTb
                {
                    Id = 2,
                    TypeName ="判断题"
                },
                new QuestTypeTb
                {
                    Id = 3,
                    TypeName ="填空题"
                },
                 new QuestTypeTb
                {
                    Id = 4,
                    TypeName ="不定项题"
                },
                  new QuestTypeTb
                {
                    Id = 5,
                    TypeName ="简答题"
                }
            });

            context.SubjectTbs.AddRange(new List<SubjectTb>{
                new SubjectTb
                {
                    Id = 1,
                    SubjectName = "语文",
                    CurriculaId=1
                },
                new SubjectTb
                {
                    Id = 2,
                    SubjectName = "数据库原理与应用教程",
                    CurriculaId=2
                }
            });

            context.BigKnowModuleTbs.AddRange(new List<BigKnowModuleTb>{
                new BigKnowModuleTb
                {
                    Id=1,
                    BigModuleName="文言文",
                    SubjectId=1
                },
                new BigKnowModuleTb
                {
                    Id = 2,
                    BigModuleName = "汉字拼音",
                    SubjectId = 1
                },
                new BigKnowModuleTb
                {
                    Id = 3,
                    BigModuleName = "语法",
                    SubjectId = 1
                },
                new BigKnowModuleTb
                {
                    Id = 4,
                    BigModuleName = "第1章 数据库系统概述",
                    SubjectId = 2
                },
                    new BigKnowModuleTb
                    {
                    Id = 5,
                    BigModuleName = "第2章 数据模型",
                    SubjectId = 2
                },
                    new BigKnowModuleTb
                    {
                    Id = 6,
                    BigModuleName = "第11章 存储过程和触发器",
                    SubjectId = 2
                }
            });

            context.SmallKnowModuleTbs.AddRange(new List<SmallKnowModuleTb>{
                new SmallKnowModuleTb
                {
                    Id=1,
                    SmallModuleName="出寨",
                    BigKnowModuleTbId=1,
                    CurriculumId = 1,
                    Keywords = "出寨"
                },
                new SmallKnowModuleTb
                {
                    Id = 2,
                    SmallModuleName = "字形判断",
                    BigKnowModuleTbId = 2,
                    CurriculumId = 1,
                    Keywords = "字形判断"
                },
                new SmallKnowModuleTb
                {
                    Id = 3,
                    SmallModuleName = "修辞手法",
                    BigKnowModuleTbId = 3,
                    CurriculumId = 1,
                    Keywords = "修辞手法"
                },
               
                new SmallKnowModuleTb
                {
                Id = 4,
                SmallModuleName = "1.1 数据库技术发展史",
                BigKnowModuleTbId = 4,
                CurriculumId = 2,
                Keywords = "人工管理\n文件系统\n数据库系统\n数据库\nDBMS\n文件系统\n冗余度"
            },
                new SmallKnowModuleTb
                {
                    Id = 5,
                    SmallModuleName = "1.2 数据库系统组成",
                    BigKnowModuleTbId = 4,
                    CurriculumId = 2,
                    Keywords = "数据库\nDBS\n组成部分\n管理系统"
                }
                ,
                new SmallKnowModuleTb
                {
                    Id =6 ,
                    SmallModuleName = "1.3 数据库的体系结构 ",
                    BigKnowModuleTbId = 4,
                    CurriculumId = 2,
                    Keywords = "DBMS\n外模式\n内模式\n数据库"
                }
                ,
                new SmallKnowModuleTb
                {
                    Id =7,
                    SmallModuleName = "2.2 概念模型",
                    BigKnowModuleTbId = 5,
                    CurriculumId = 2,
                    Keywords = "概念模型\n实体集\n数据库\n"
                },
                new SmallKnowModuleTb
                {
                    Id =8,
                    SmallModuleName = "2.3 数据模型",
                    BigKnowModuleTbId = 5,
                    CurriculumId = 2,
                    Keywords = "层次模型\n数据结构\n关系模型\n关键字\n独立性"
                },
                new SmallKnowModuleTb
                {
                    Id =9,
                    SmallModuleName = "11.1 存储过程",
                    BigKnowModuleTbId = 6,
                    CurriculumId = 2,
                    Keywords = "灵活性\nSQL\nSERVER\n自定义\n笛卡儿"
                },
                new SmallKnowModuleTb
                {
                    Id =10,
                    SmallModuleName = "11.2 触发器 ",
                    BigKnowModuleTbId = 6,
                    CurriculumId = 2,
                    Keywords = "触发器\n不起作用\n完整性\nDML\n触发器\nSELECT"
                }

            });

            context.QuestionTbs.AddRange(new List<QuestionTb> {
                new QuestionTb
                {
                    Id=1,
                    QuestName="出塞的后两句是什么？",
                    QuestTypeTbId=3,
                    Keywords="出塞",
                    SmallKnowModuleTbId=1,
                    QuestLevel=3,
                    QuestAnalysis = "秦时明月汉时关，万里长征人未还。但使龙城飞将在，不教胡马度阴山。",
                    TeacherId = user.Id
                },
                new QuestionTb
                {
                    Id = 2,
                    QuestName = "选出字形有误的一项",
                    QuestTypeTbId = 1,
                    Keywords = "字形",
                    SmallKnowModuleTbId = 2,
                    QuestLevel = 4,
                    QuestAnalysis = "冥思遐想 =》 冥思苦想",
                    TeacherId = user.Id
                },
                new QuestionTb
                {
                    Id = 3,
                    QuestName ="下列句子修辞方法运用不恰当的一项是",
                    QuestTypeTbId = 1,
                    Keywords = "修辞方法",
                    SmallKnowModuleTbId = 3,
                    QuestLevel = 2,
                    TeacherId = user.Id
                },
                new QuestionTb
                {
                    Id = 4,
                    QuestName = "没有错别字的一项",
                    QuestTypeTbId = 1,
                    Keywords = "字形",
                    SmallKnowModuleTbId = 3,
                    QuestLevel = 3,
                    QuestAnalysis = "B炫，C援，D稳",
                    TeacherId = user.Id
                }
            });

            context.BlanksTbs.AddRange(new List<BlanksTb>
            {
                new BlanksTb
                {
                    Id=1,
                    CorrectValue="但使龙城飞将在，不教胡马度阴山。",
                    QuestTypeTbId = 3,
                    QuestionTbId = 1
                }
            });

            context.SingleChoiceTbs.AddRange(new List<SingleChoiceTb>
            {
                new SingleChoiceTb
                {
                    Id = 1,
                    Options = "A",
                    Value = "流言蜚语 不可名状 油光可鉴 心无旁骛",
                    CorrectOption = "0",
                    QuestionTbId = 2,
                    QuestTypeTbId = 1
                },
                new SingleChoiceTb
                {
                    Id =2,
                    Options = "B",
                    Value ="相辅相成 故弄玄虚 诚惶诚恐 正襟危坐",
                    CorrectOption = "0",
                    QuestionTbId = 2,
                    QuestTypeTbId = 1
                },
                new SingleChoiceTb
                {
                    Id =3,
                    Options = "C",
                    Value ="戛然而止 惟妙惟肖 略胜一筹 冥思遐想",
                    CorrectOption = "1",
                    QuestionTbId = 2,
                    QuestTypeTbId = 1
                },
                new SingleChoiceTb
                {
                    Id =4,
                    Options = "D",
                    Value ="莫衷一是 头晕目眩 沧海桑田 花团锦簇",
                    CorrectOption = "0",
                    QuestionTbId = 2,
                    QuestTypeTbId = 1
                },
                new SingleChoiceTb
                {
                    Id = 5,
                    Options = "A",
                    Value = "茫茫的草原上，嫩绿的小草编织出色彩斑斓的毯子，一直铺展到天边。",
                    CorrectOption = "1",
                    QuestionTbId =3,
                    QuestTypeTbId = 1
                },
                new SingleChoiceTb
                {
                    Id = 6,
                    Options = "B",
                    Value = "知识如沙石下面的泉水，掘得越深越清澈。",
                    CorrectOption = "0",
                    QuestionTbId =3,
                    QuestTypeTbId = 1
                },
                new SingleChoiceTb
                {
                    Id = 7,
                    Options = "C",
                    Value = "姑娘从泉边汲水回来了，辫梢上沾着几滴水珠，欢乐盛开在她的眼睛和眉毛上。",
                    CorrectOption = "0",
                    QuestionTbId =3,
                    QuestTypeTbId = 1
                },
                new SingleChoiceTb
                {
                    Id = 8,
                    Options = "D",
                    Value = "习习凉风在树叶间演奏着优美动人的小夜曲。",
                    CorrectOption = "0",
                    QuestionTbId =3,
                    QuestTypeTbId = 1
                },
                new SingleChoiceTb
                {
                    Id = 9,
                    Options = "A",
                    Value ="一尘不染 鲜为人知 正襟危坐 锋芒毕露",
                    CorrectOption = "1",
                    QuestionTbId = 4,
                    QuestTypeTbId = 1
                },
                new SingleChoiceTb
                {
                    Id = 10,
                    Options = "B",
                    Value ="当之无愧 头晕目炫 广袤无垠 开卷有益",
                    CorrectOption = "0",
                    QuestionTbId = 4,
                    QuestTypeTbId = 1
                },
                new SingleChoiceTb
                {
                    Id = 11,
                    Options = "C",
                    Value ="家喻户晓 有例可援 深恶痛疾 简明扼要",
                    CorrectOption = "0",
                    QuestionTbId = 4,
                    QuestTypeTbId = 1
                },
                new SingleChoiceTb
                {
                    Id = 12,
                    Options = "D",
                    Value ="随机应变 悠然自在 优胜劣汰 十拿九紊",
                    CorrectOption = "0",
                    QuestionTbId = 4,
                    QuestTypeTbId = 1
                }
            }) ;

            context.SaveChanges();

            //InitAdmin(context);
            base.Seed(context);
        }
    }
}